package com.bjtang;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

import org.json.JSONException;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.bjtang.test.User;
import com.bjtang.util.MapList;

@SpringBootApplication
@EnableScheduling
@Component
public class BjtangScheduledApplication {

	public static void main(String[] args) {
		SpringApplication.run(BjtangScheduledApplication.class, args);

		// 完成初始化数据导入
		String fileName = "lib/班组.txt";
		String fileName2 = "lib/监理.txt";
		String fileName3 = "lib/管理.txt";
		try {
			mapListInit(fileName, "班组");
			mapListInit(fileName2, "监理");
			mapListInit(fileName3, "管理");
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		User cacheData = MapList.getInstance().getCacheData("9588111171");
	
		
		if (cacheData != null) {
			System.err.println(cacheData.getId() + "  " + cacheData.getName() + "  " + cacheData.getPosition());
			MapList.getInstance().addInUser(cacheData);
			MapList.getInstance().addOutUser(cacheData);
			MapList.getInstance().addInUser(cacheData);
			MapList.getInstance().addOutUser(cacheData);
		} else {
			System.err.println("没有此用户记录!");
		}

		User cacheData2 = MapList.getInstance().getCacheData("95881111");
		if (cacheData2 != null) {
			System.err.println(cacheData2.getId() + "  " + cacheData2.getName() + "  " + cacheData2.getPosition());
		} else {
			User user = new User();
			user.setId("95881111");
			user.setName("未知姓名");
			user.setPosition("未知职务");
			MapList.getInstance().addInUser(user);
			System.err.println("95881111" + "  未知姓名  未知职务");
		}
		
		User cacheData3 = MapList.getInstance().getCacheData("8373062669");
		
		MapList.getInstance().addInUser(cacheData3);
		
		User cacheData4 = MapList.getInstance().getCacheData("6777111128");
		
		MapList.getInstance().addInUser(cacheData4);
	}

	// 清理mapList
	@Scheduled(cron = "0 0 2 * * ?") // 每天凌晨两点触发
	public void execute() {
		// SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //设置日期格式
		MapList.getInstance().removeInUserAll();
		MapList.getInstance().removeOutUserAll();
		// System.out.println(" " + df.format(new Date()));
	}

	// TODO
	// 每30秒查询一次是否有新的事件
	// 清理mapList
	@Scheduled(cron = "0/40 * * * * ?") // 每隔40秒执行一次
	public void execute2() {
		// SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //设置日期格式

		try {
			Scanner sc = new Scanner(new FileReader("lib/设备.txt"));
			while (sc.hasNextLine()) { // 按行读取字符串
				String line = sc.nextLine();
				System.out.println(line);
				String[] split = line.split(",");
				AcsMain.main(split);
			}

		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// System.out.println(" " + df.format(new Date()));
	}

	public static void mapListInit(String fileName, String position) throws FileNotFoundException {
		try (Scanner sc = new Scanner(new FileReader(fileName))) {
			while (sc.hasNextLine()) { // 按行读取字符串
				String line = sc.nextLine();
				System.out.println(line);
				List<String> result = Arrays.asList(line.split(","));
				User user = new User();
				user.setName(result.get(0));
				user.setId(result.get(1));
				user.setPosition(position);
				MapList.getInstance().addCacheData(result.get(1), user);
			}
		}
	}

}
